import type { ReactNode } from 'react';
import { Navigate, useLocation } from 'react-router-dom';
import { AxiosManager } from '../utils/axiosInstance';

interface ProtectedRouteProps {
  children: ReactNode;
}

const ProtectedRoute = ({ children }: ProtectedRouteProps) => {
  const location = useLocation();
  const isAuthenticated = AxiosManager.isAuthenticated();

  if (!isAuthenticated) {
    // 重定向到登录页，但保留当前路径以便登录后返回
    return <Navigate to="/login" state={{ from: location }} replace />;
  }

  return children;
};

export default ProtectedRoute;